//
//  XFDRegisterTwoView.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/6.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDRegisterTwoView.h"
#import "XFDLogInInputView.h"
#import <Masonry/Masonry.h>
#import <XFDCommon/HBCommon.h>
#import <BlocksKit/UIControl+BlocksKit.h>

@interface XFDRegisterTwoView ()

@property (nonatomic, strong)XFDLogInInputView *inputView;
@property (nonatomic, strong)UIButton *sureButton;
@property (nonatomic, strong)UILabel *titleNavLabel;
@property (nonatomic, strong)UILabel *titleLabel;
@end
@implementation XFDRegisterTwoView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)inputViewResignFirstResponder{
    [self.inputView inputViewResignFirstResponder];
}
- (void)inputViewBecomeFirstResponder{
    [self.inputView inputViewBecomeFirstResponder];
}
-(void)openCodeBtnCountdown{
    [self.inputView openCountdown];
}
- (void)createView{
    
    [self addSubview:self.inputView];
    [self addSubview:self.sureButton];
    [self addSubview:self.titleNavLabel];
    [self addSubview:self.titleLabel];
    [self.titleNavLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_offset(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleNavLabel.mas_bottom).with.offset(40);
        make.left.equalTo(self.mas_left).with.offset(40);
        make.right.equalTo(self.mas_right);
        make.height.mas_offset(40);
    }];
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.right.equalTo(self.mas_right);
        make.left.equalTo(self.mas_left);
        make.height.mas_offset(50);
    }];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputView.mas_bottom).with.offset(50);
        make.right.equalTo(self.mas_right).with.offset(-40);
        make.left.equalTo(self.mas_left).with.offset(40);
        make.height.mas_offset(40);
    }];
    WEAKSELF
    self.inputView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        if (str.length >0 ) {
            strongSelf.sureButton.userInteractionEnabled = YES;
            strongSelf.sureButton.backgroundColor = GlobalBuleColor;
        }else{
            strongSelf.sureButton.userInteractionEnabled = NO;
            strongSelf.sureButton.backgroundColor = GlobalGrayOneColor;
        }
    };

    self.inputView.getCodeBlock = ^{
       STRONGSELF
        if (strongSelf.getCodeBlock) {
            strongSelf.getCodeBlock();
        }
    };
}

- (UILabel *)titleNavLabel{
    if (!_titleNavLabel) {
        _titleNavLabel = [[UILabel alloc]init];
        _titleNavLabel.text = @"";
        _titleNavLabel.textAlignment = NSTextAlignmentCenter;
        _titleNavLabel.font = [UIFont systemFontOfSize:16];
    }
    return _titleNavLabel;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"验证手机号";
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _titleLabel;
}

- (XFDLogInInputView *)inputView{
    if (!_inputView) {
        _inputView =  [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:YES];
        _inputView.placeholder = @"请输入短信验证码";
    }
    return _inputView;
}

- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"下一步" forState:UIControlStateNormal];
        _sureButton.backgroundColor = GlobalGrayOneColor;
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sureButton.layer.cornerRadius = 5;
        _sureButton.layer.masksToBounds = YES;
        _sureButton.userInteractionEnabled = NO;
        WEAKSELF
        [_sureButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            if (strongSelf.nextBlock) {
                strongSelf.nextBlock(strongSelf.inputView.inputText);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}
@end
